package com.gy.controller.Form;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gy.controller.common.UserHolder;
import com.gy.entity.Form.FormEntity;
import com.gy.result.Result;
import com.gy.service.FormService;

import com.gy.vo.form.FormMemberVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;  // 正确的导入

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api/forms")
@CrossOrigin
@Api(tags ="空间接口")
public class FormController {
    @Autowired
    private FormService formService;

    @PostMapping("/save")
    @ApiOperation("新增表单")
    public Result saveForm(@RequestBody FormEntity form) {

        return formService.saveForm(form);

    }

    @GetMapping("/list/{id}")
    @ApiOperation("获取表单数据")
    public Result getFormList(@PathVariable Long id) {

        FormEntity forms = formService.getFormsByUserId(id);
        return Result.success(forms);
    }

    @PostMapping("/submit")
    @ApiOperation("提交表单数据（更新表单）")
    public Result submitForm(@RequestBody FormEntity form) {

       return formService.updateFromById(form);

    }

    @GetMapping("/detail/{id}")
    @ApiOperation("获取已提交的表单数据")
    public Result getFormDetail(@PathVariable Long id) {
        FormEntity form = formService.getById(id);
        if (form == null) {
            return Result.error("表单不存在");
        }
        return Result.success(form);
    }

    @GetMapping("/listPaged")
    @ApiOperation("分页获取用户的表单数据")
    public Result getPagedFormList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "5") int size) {


        IPage<FormEntity> formPage = formService.getFormsByUserIdPaged( page, size);
        return Result.success(formPage);
    }

    @PutMapping("/rename/{id}")
    @ApiOperation("重命名空间")
    public Result renameForm(@PathVariable Long id, @RequestParam String newName) {
        Long userId = UserHolder.getUser().getUserId();
        FormEntity form = formService.getById(id);

        if (form == null) {
            return Result.error("表单不存在");
        }



        form.setName(newName);
        formService.updateById(form);

        return Result.success("重命名成功");
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除空间")
    public Result deleteForm(@PathVariable Long id) {

        return formService.deleteForm(id);
    }


    /**
     * 给某个空间添加成员
     */
    @PostMapping("/addMember/{id}")
    @ApiOperation("给某个空间添加成员")
    public Result addMember(@PathVariable Long id, @RequestParam Long userId) {
        Long userId1 = UserHolder.getUser().getUserId();

        // 检查是否尝试添加自己
        if (userId.equals(userId1)) {
            return Result.error("不能添加自己为成员");
        }

        return formService.addMember(id, userId);
    }


    /**
     * 查询某个空间里面的全部成员
     */
    @GetMapping("/listMembers/{id}")
    @ApiOperation("查询某个空间里面的全部成员")
    public Result<List<FormMemberVO>> listMembers(@PathVariable Long id) {

        return formService.listMembers(id);
    }


    /**
     * 删除某个空间里面的某个成员
     */
    @DeleteMapping("/deleteMember/{id}")
    @ApiOperation("删除某个空间里面的某个成员")
    public Result deleteMember(@PathVariable Long id, @RequestParam Long userId) {
        Long userId1 = UserHolder.getUser().getUserId();
        // 检查是否尝试删除自己
        if (userId.equals(userId1)) {
            return Result.error("不能删除自己");
        }
        return formService.deleteMember(id, userId);
    }


    /**
     * 更改成员角色
     */
    @PutMapping("/updateMemberRole/{id}")
    @ApiOperation("更新成员角色")
    public Result updateMemberRole(@PathVariable Long id, @RequestParam Long userId, @RequestParam String role) {

            Long userId1 = UserHolder.getUser().getUserId();
            // 检查是否尝试修改自己
            if (userId.equals(userId1)) {
                return Result.error("不能修改自己的角色");
            }
            return formService.updateMemberRole(id, userId, role);

    }

    private static Map<String, String> map = new HashMap<>();

    @RequestMapping("/oom")
    public String oom() throws Exception {
        for (int i = 0; i < 100000; i++) {
            map.put("key" + i, "value" + i);
        }
        return "oom";
    }
}
